package lex.dm.ui.actions;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;

import org.lex.swing.ActionBuilder;
import org.lex.swing.table.TableHelper;
import org.lex.swing.util.Buttons;
import org.lex.swing.util.CenterSouthPane;
import org.lex.swing.widgets.EnhancedDialog;

import lex.dm.DTree;
import lex.dm.utils.DBMDUtils;

public class DBMSMetaDataAction extends ActionBuilder
{
	public DBMSMetaDataAction()
	{
		this.setName( "Meta Data" );
		this.setEnabled( false );
	}

	@Override
	public void actionPerformed( ActionEvent e )
	{
		Window root = DTree.getApplication().getRootWindow();
		final EnhancedDialog dialog = new EnhancedDialog( root, "Meta Data", false );
		JTable table = new JTable( TableHelper.createModel( DBMDUtils.getMetaData( DTree
			.getApplication().getConnection() ) ) );
		table.setAutoCreateRowSorter( true );
		table.getRowSorter().setSortKeys( Collections.singletonList( new RowSorter.SortKey( 0, SortOrder.ASCENDING )  ) );
		table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
		table.getColumnModel().getColumn( 0 ).setPreferredWidth( 200 );
		table.getColumnModel().getColumn( 1 ).setPreferredWidth( 230 );
		Buttons buttons = Buttons.trailing( new ActionBuilder( "OK" )
			.setActionListener( new ActionListener(){
				public void actionPerformed( ActionEvent e )
				{
					dialog.dispose();
				}
			} ) );
		dialog.setContentPane( new CenterSouthPane( new JScrollPane( table ), buttons ) );
		dialog.setDefaultButton( buttons.getButton( 0 ) );
		dialog.setSize( 450, 400 );
		dialog.setLocationRelativeTo( root );
		dialog.setVisible( true );
	}
}
